package com.edu.exam.entity.exampaper.generation;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 组卷任务实体类
 * 所属模块：B-03 试卷管理模块
 * 对应数据库表：generation_task
 *
 * <p>组卷任务实体类，用于管理自动组卷任务的执行过程。
 * 支持任务队列管理、进度跟踪、结果输出等功能。</p>
 *
 * @author System
 * @version 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
public class GenerationTask {

    /**
     * 任务ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 任务UUID
     */
    @Column(name = "task_id", nullable = false, length = 32, unique = true)
    private String taskId;

    /**
     * 方案ID
     */
    @Column(name = "scheme_id", nullable = false, length = 32)
    private String schemeId;

    /**
     * 试卷标题
     */
    @Column(name = "title", nullable = false, length = 200)
    private String title;

    /**
     * 试卷描述
     */
    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    /**
     * 配置ID
     */
    @Column(name = "config_id", length = 32)
    private String configId;

    /**
     * 算法类型
     */
    @Column(name = "algorithm_type", nullable = false, length = 30)
    private String algorithmType;

    /**
     * 任务状态
     */
    @Column(name = "status", nullable = false, length = 20)
    private String status = "PENDING";

    /**
     * 任务优先级
     */
    @Column(name = "priority", nullable = false)
    private Integer priority = 0;

    /**
     * 创建者ID
     */
    @Column(name = "creator_id", nullable = false, length = 32)
    private String creatorId;

    /**
     * 请求者ID
     */
    @Column(name = "requester_id", length = 32)
    private String requesterId;

    /**
     * 开始时间
     */
    @Column(name = "start_time")
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    @Column(name = "end_time")
    private LocalDateTime endTime;

    /**
     * 预估时间（秒）
     */
    @Column(name = "estimated_time")
    private Integer estimatedTime;

    /**
     * 实际时间（秒）
     */
    @Column(name = "actual_time")
    private Integer actualTime;

    /**
     * 队列位置
     */
    @Column(name = "queue_position")
    private Integer queuePosition;

    /**
     * 重试次数
     */
    @Column(name = "retry_count", nullable = false)
    private Integer retryCount = 0;

    /**
     * 最大重试次数
     */
    @Column(name = "max_retries", nullable = false)
    private Integer maxRetries = 3;

    // ===== 审计字段 =====

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    // ===== 常量定义 =====

    /** 任务状态 - 待执行 */
    public static final String STATUS_PENDING = "PENDING";

    /** 任务状态 - 执行中 */
    public static final String STATUS_RUNNING = "RUNNING";

    /** 任务状态 - 选择阶段 */
    public static final String STATUS_SELECTION_PHASE = "SELECTION_PHASE";

    /** 任务状态 - 布局阶段 */
    public static final String STATUS_LAYOUT_PHASE = "LAYOUT_PHASE";

    /** 任务状态 - 预览阶段 */
    public static final String STATUS_PREVIEW_PHASE = "PREVIEW_PHASE";

    /** 任务状态 - 已完成 */
    public static final String STATUS_COMPLETED = "COMPLETED";

    /** 任务状态 - 失败 */
    public static final String STATUS_FAILED = "FAILED";

    /** 任务状态 - 已取消 */
    public static final String STATUS_CANCELLED = "CANCELLED";
}